package org.gnyxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.gnyxy.constant.DeleteConstant;
import org.gnyxy.constant.MessageConstant;
import org.gnyxy.dto.AddressDicPageQueryDTO;
import org.gnyxy.entity.AddressDic;
import org.gnyxy.exception.BaseException;
import org.gnyxy.mapper.AddressDicMapper;
import org.gnyxy.result.PageResult;
import org.gnyxy.service.AddressDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressDicServiceImpl implements AddressDicService {
    @Autowired
    private AddressDicMapper addressDicMapper;
    @Override
    public void add(AddressDic addressDic) {
        if (addressDic == null){
            throw new BaseException(MessageConstant.OBJECT_IS_NULL);
        }
        addressDic.setDeleted(DeleteConstant.IS_NOT_DELETED);
        addressDicMapper.insert(addressDic);
    }

    @Override
    public void deleteById(Long id) {
        if (id == null){
            throw new BaseException(MessageConstant.ID_IS_NULL);
        }
        AddressDic addressDic = addressDicMapper.getById(id);
        addressDic.setDeleted(DeleteConstant.IS_DELETED);
        addressDicMapper.update(addressDic);
    }

    @Override
    public AddressDic getById(Long id) {
        if (id == null){
            throw new BaseException(MessageConstant.ID_IS_NULL);
        }
        return addressDicMapper.getById(id);
    }

    @Override
    public void update(AddressDic addressDic) {
        if (addressDic == null){
            throw new BaseException(MessageConstant.OBJECT_IS_NULL);
        }
        addressDicMapper.update(addressDic);
    }

    @Override
    public PageResult page(AddressDicPageQueryDTO addressDicPageQueryDTO) {
        PageHelper.startPage(addressDicPageQueryDTO.getPage(),addressDicPageQueryDTO.getPageSize());
        Page<AddressDic> addressDics = addressDicMapper.pageQuery(addressDicPageQueryDTO);
        return new PageResult(addressDics.getTotal(),addressDics.getResult());
    }

    /**
     * 获取所有的区县
     * @return
     */
    @Override
    public List<String> getDistrict() {
        return addressDicMapper.getDistrict();
    }

    @Override
    public List<String> getStreet(String districtName) {
        return addressDicMapper.getStreet(districtName);
    }

    @Override
    public List<String> getVillage(String districtName, String streetName) {
        return addressDicMapper.getVillage(districtName,streetName);
    }
}
